﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Data;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class BillList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                BindBillList(1);
                performanceDate.SelectedDate = System.DateTime.Now;
            }
        }

        protected void RepeaterBills_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindBillList(1);
            }

            if (e.CommandName == "update")
            {
                Label lblcBVCode = e.Item.FindControl("lblcBVCode") as Label;
                Label lbliDueDte = e.Item.FindControl("lbliDueDte") as Label;
                txtBillNo.Text = lblcBVCode.Text;
                dDueDate.SelectedDate = DateTime.ParseExact(lbliDueDte.Text, "yyyy/MM/dd", null);
                
            }

        }

        private void BindBillList(int pageindex)
        {
            string Whereclause = "";
            string orderby = ddlOrderby.SelectedValue.ToString()+" Asc";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause = " and cCusCode in ( select CustomerCode from Cms_Ctrt_Customer where CustomerName like '%" + txtContractCodeBasic.Text.Trim() + "%' or CustomerAddress like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (txtcBVCode.Text.Trim() != "")
            {
                Whereclause = " and cBVCode like '"  + txtcBVCode.Text.Trim() + "%'";
            }
            if (txtdDateFrom.SelectedDate != null) {
                Whereclause += " and dDate >= '" + txtdDateFrom.SelectedDate.Value.ToString() + "'";
            }
            if (txtdDateTo.SelectedDate != null)
            {
                Whereclause += " and dDate <= '" + txtdDateTo.SelectedDate.Value.ToString() + "'";
            } if (txtAmountFrom.Text.Trim() != null && txtAmountFrom.Text.Trim() != "")
            {
                Whereclause += " and iNatSumTotal >= " + txtAmountFrom.Text.Trim() + "";
            }
            if (txtAmountTo.Text.Trim() != null && txtAmountTo.Text.Trim() != "")
            {
                Whereclause += " and iNatSumTotal <= " + txtAmountTo.Text.Trim() + "";
            }
            
            if (txtdidDueDteFrom.SelectedDate != null)
            {
                Whereclause += " and idDueDte > '" + txtdidDueDteFrom.SelectedDate.Value.ToString() + "'";
            }
            if (txtdidDueDteTo.SelectedDate != null)
            {
                Whereclause += " and idDueDte <= '" + txtdidDueDteTo.SelectedDate.Value.ToString() + "'";
            }
           
            int RecordCount = 0;
            DataTable list = new BillBll().QueryBill(Whereclause, orderby, AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterBills.DataSource = list;
            RepeaterBills.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterBills_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblcustomerCode = e.Item.FindControl("lblcustomerCode") as Label;
            Label lblCustomerName = e.Item.FindControl("lblCustomerName") as Label;

            if (lblcustomerCode != null && lblCustomerName != null)
            {
                CtrtCustomerEntity customerEntity = new CtrtCustomerBll().Get<CtrtCustomerEntity>(" and CustomerCode = '"+lblcustomerCode.Text+"'");
                if (customerEntity != null)
                {
                    lblCustomerName.Text = customerEntity.CustomerName;
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindBillList(1);
        } 

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindBillList(AspNetPager1.CurrentPageIndex);
        }

        protected void ddlOrderby_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindBillList(1);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int rtn;

            if (txtBillNo.Text.Trim() == "" || !dDueDate.SelectedDate.HasValue) { return; }
            BillBll bll = new BillBll();

            rtn = bll.updateBillDueDate(txtBillNo.Text.Trim(),dDueDate.SelectedDate.ToString());
            BindBillList(1);
        }

        protected void btnCalc_Click(object sender, EventArgs e)
        {

            if (!performanceDate.SelectedDate.HasValue) { return; }

            //new BillBll().SetBillPenaltyAwardForBusinessor(new DateTime(performanceDate.SelectedDate.Value.AddMonths(0).Year, performanceDate.SelectedDate.Value.AddMonths(0).Month, 1).AddDays(-1));
            new BillBll().SetBillPenaltyAwardForBusinessor( System.Convert.ToDateTime(performanceDate.SelectedDate.ToString()));
            
            ClientScript.RegisterStartupScript(this.GetType(), "save", "initMsg('更新已完成。');", true);

        } 
    }
}
